from Page.UserPage.ErpLoginPage import ErpLoginPage
from selenium.webdriver.common.by import By as by
import time



class MultilevelAudit(ErpLoginPage):
    """表现层"""
    # 系统管理，多级审核打开前
    system_manage_before = (by.XPATH, '//*[@id="app"]/section/aside/div/ul/li[14]/div')

    # 系统配置，多级审核打开前
    system_init_before = (by.XPATH, '//*[@id="app"]/section/aside/div/ul/li[14]/ul/li[5]')

    # 系统管理，多级审核打开后
    system_manage_after = (by.XPATH, '//*[@id="app"]/section/aside/div/ul/li[15]/div[1]')

    # 系统配置，多级审核打开后
    system_init_after = (by.XPATH, '//*[@id="app"]/section/aside/div/ul/li[15]/ul/li[5]')

    # 多级审核，需要刷新
    ma_button = (by.XPATH, '//*[@id="app"]/section/section/main/div[2]/div/div/div/div/form/div[10]/div[2]/div/div[2]/div/span/button[1]')

    # 流程类型
    process_type = (by.XPATH,'//*[@id="app"]/section/section/main/div[2]/div/div/div/div/form/div[10]/div[2]/div/div[2]/div/span/div/div/div/ul/li/div/input')

    # 零售管理
    retail_management = (by.XPATH, '(//div[@class="ant-menu-submenu-title"])[2]')

    # 零售出库
    retail_delivery = (by.XPATH, '//a[@target="零售出库"]')

    # 新增按钮
    new_order = (by.CSS_SELECTOR, '.table-operator .ant-btn-primary')

    # 点击引导关闭
    skip_button = (by.CSS_SELECTOR, '.introjs-skipbutton')

    # 会员卡号点击
    click_vip = (by.CSS_SELECTOR, '#organId')

    # 会员卡号输入框
    input_vip = (by.CSS_SELECTOR, '#organId .ant-select-search__field')

    # 选择商品,js点击
    select_goods = (by.XPATH, '//span[@class="ant-input-group ant-input-group-compact"]/button')

    # 选择最新商品
    new_product = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr.ant-table-row:nth-of-type(1)')

    # 查看刚刚的库存，只获取了文本
    data_num = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr:nth-of-type(1)>td:nth-of-type(10)')

    # 订单确定按钮
    determine = (by.XPATH, '/html/body/div[5]/div/div[2]/div/div[2]/div[3]/div/button[2]')

    # 保存
    save_check = (by.CSS_SELECTOR, 'div.ant-modal-footer>button:nth-of-type(3)')

    # 刚创建的订单的编辑按钮
    edit_order = (by.CSS_SELECTOR,'#app > section > section > main > div:nth-child(3) > div > div > div > div > div:nth-child(3) > div > div > div > div > div > div > div.ant-table-body > table > tbody > tr:nth-child(1) > td:nth-child(2) > span > a:nth-child(3)')

    # 提交流程，后面等2秒
    submit_process = (by.CSS_SELECTOR, 'div.ant-modal-footer>button:nth-of-type(4)')

    # 提交流程确定
    process_determine = (by.XPATH,'//*[@id="LSCK"]/div[2]/div/div[2]/div[2]/div[6]/div/div/div[2]/div/div[2]/div[3]/button[2]')

    # 任务审核
    assignment_audit = (by.CSS_SELECTOR,'#app > section > aside > div > ul > li:nth-child(2) > div')

    # 代办任务
    assignment_will_do = (by.CSS_SELECTOR,'[target="待办任务"]')

    # 切换到代办任务框架内
    assignment_frame = (by.CSS_SELECTOR,'#\/system\/plugins\/workflow\/workflowToDoTask\.html:nth-child(1)')

    # 查询
    assignment_select = (by.XPATH,'/html/body/div[1]/div/div[1]/form/div/div[3]/button[1]')

    # 选择最新订单处理按钮
    assignment_handle = (by.CSS_SELECTOR,'body > div.card-area.ant-card > div > div:nth-child(2) > div > div > div > div > div > div > table > tbody > tr:nth-child(1) > td:nth-child(2) > span > a:nth-child(1)')

    # 点击保存
    assignment_save = (by.CSS_SELECTOR,'body > div:nth-child(11) > div > div.ant-modal-wrap.ant-modal-cust-warp > div > div.ant-modal-content > div.ant-modal-footer > div > button.ant-btn.ant-btn-primary')


    """操作层"""

    def open_ma(self):
        """打开或关闭多级审核"""
        self.scroll_bar(0,1000)
        self.click(self.system_manage_before)
        time.sleep(1)
        self.click(self.system_init_before)
        self.click(self.ma_button)
        self.js_click(self.process_type)

        self.send_keys(self.process_type,"零售出库")
        self.send_keys_enter(self.process_type)
        self.send_keys(self.process_type,"零售退货")
        self.send_keys_enter(self.process_type)
        self.refresh()
        time.sleep(2)

    def close_ma(self):
        """打开或关闭多级审核"""
        self.js_click(self.system_manage_after)
        time.sleep(1)
        self.js_click(self.system_init_after)
        self.click(self.ma_button)
        self.refresh()
        time.sleep(2)


    def open_add_page(self):
        """进入新增页面"""
        self.click(self.retail_management)
        time.sleep(1)
        self.click(self.retail_delivery)
        self.click(self.new_order)
        self.click(self.skip_button)
        time.sleep(1)

    def post_order(self):
        """填写商品信息，返回库存并提交审核"""
        self.js_click(self.click_vip)  # 不点不让输
        self.send_keys(self.input_vip, "12314")
        time.sleep(1)
        self.js_click(self.select_goods)
        self.js_click(self.new_product)
        text = self.get_element_text(self.data_num)
        num = int(text)
        self.js_click(self.determine)
        time.sleep(1)
        self.click(self.save_check)
        time.sleep(1)
        self.js_click(self.edit_order)  # Not interactable
        time.sleep(1)
        self.click(self.submit_process)
        time.sleep(1)
        self.click(self.process_determine)
        return num

    def assignment_audit_process(self):
        """审核新创建的出库订单，同意"""
        self.click(self.assignment_audit)
        time.sleep(1)
        self.click(self.assignment_will_do)
        time.sleep(2)

        # 切换到当前框架
        assignment_frame_element = self.driver.find_element(*self.assignment_frame)
        self.driver.switch_to.frame(assignment_frame_element)
        """
            为什么二次封装的find_element不行，driver的就可以？
            可能是返回值的问题
        """
        self.js_click(self.assignment_select)
        self.js_click(self.assignment_handle)
        time.sleep(2)
        self.click(self.assignment_save)
        # 切回父框架
        self.driver.switch_to.parent_frame()

    def get_inventory(self):
        """获取库存量"""
        self.click(self.retail_management)
        time.sleep(1)
        self.click(self.retail_delivery)
        self.click(self.new_order)
        self.js_click(self.select_goods)
        text = self.get_element_text(self.data_num)
        time.sleep(1)
        num = int(text)
        return num

if __name__ == '__main__':
    ma = MultilevelAudit()

    ma.common_login()
    ma.open_ma()
    ma.open_add_page()
    num1 = ma.post_order()
    ma.assignment_audit_process()
    num2 = ma.get_inventory()
    ma.refresh()
    ma.close_ma()
    assert num1 - num2 == 1


